<?
require("../../common/init.php");
require("author.php");
require("upfile.php");

$gee=$configuration; // gee, 意为赶车时候喊的"驾", 此处用来表示次页面的主角对象
$gee->rows=15;
$cate=$ad_category; // cate = category, 分类
$cateid=0;

$title='Logo';
$url='logo';
$do=isset($_GET["do"])?clean($_GET["do"]):'view'; // 操作: 查看/编辑/删除 
$action=isset($_POST["action"])?clean($_POST["action"]):''; // 数据库操作: CURD 
$id=0; if (isset($_GET["id"])) $id = clean($_GET["id"]);

// sql action / sql 动作
switch ($action){
	case "update":
		sql_update();
		break;
	case "add":
		sql_insert();
		break;
}

if($id==0){
	$page=1; if(isset($_GET["page"]))if(is_numeric($_GET["page"]))$page=$_GET["page"];
	$cateid=0;
	$order='seq_no';
	$where=array();
	$where[]=array('name'=>'module','val'=>'logo','opn'=>'=');
	$pages=$gee->count_pages($where);
	$info=$gee->get_page($page,$where,$order);
}
else $info=$gee->get_row($id);

function add()
{
	global $url;
	$row = array();
	echo '<form enctype="multipart/form-data" method="post" action="'.$url.'.php">';
	show_box($row, true, true);
	echo '<input type="hidden" name="action" value="add"></form>';
}

function update($info)
{
	global $id,$gee,$url;
	if($id=="") 
	{
		echo "Invali argument!!!";
		exit;
	}
	echo '<form enctype="multipart/form-data" method="post" action="'.$url.'.php">';
  	show_box($info, true, false);
	echo '<input type="hidden" name="action" value="update"></form>';
}
function show_info($info)
{
	global $gee,$url,$id;
	if($id==0)
	{
  		show_boxes($info);
	}
	else 
	{
		show_box($info, false);
	}
}
function sql_insert()
{
	global $gee,$url;
	$pic=upfile($_FILES["cpic"]);
	$arr=array(
		'name'=>iso(@$_POST["name"]),
		'pic'=>$pic,
		'link'=>iso(@$_POST["link"]),
		'category_id'=>iso(@$_POST["category_id"]),
		'seq_no'=>iso(@$_POST["seq_no"]),
	);
	if($gee->insert($arr))
	echo '<script type="text/javascript">alert('.ROOT_ADD_SUCCESS.');location="'.$url.'.php";</script>';
}
function sql_update()
{
	global $gee, $url;
	$candelete=0;
	$picdir=ROOT.'upload/';
	$id=iso(@$_POST["id"]);
	$arr=array();

	if($_FILES["cpic"]["size"] > 0){
		$info=$gee->get_row($id);
		$oldpic=$picdir.$info['value'];
		$candelete=1;
		$pic=upfile($_FILES["cpic"]);
		$arr['value']=$pic;

		if($gee->update($arr, 'id', $id)){
			if($candelete) @unlink($oldpic);
			yell(ROOT_SUCCESS, $url.'.php');
		}
	}
}

// view 加载视图
require ROOTVIEW.$url.'.php';